package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    private final UserMapper userMapper;

    private final OrderMapper orderMapper;
    private final SetmealMapper setmealMapper;
    private final DishMapper dishMapper;

    @Autowired
    public WorkspaceServiceImpl(UserMapper userMapper, OrderMapper orderMapper, SetmealMapper setmealMapper, DishMapper dishMapper) {
        this.userMapper = userMapper;
        this.orderMapper = orderMapper;
        this.setmealMapper = setmealMapper;
        this.dishMapper = dishMapper;
    }


    /**
     * 查询今日运营数据
     *
     * @return vo
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        Integer newUsers = userMapper.countByTime(map);//新增用户数
        Integer totalOrderNumber = orderMapper.countByMap(map); //每日订单总数
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumByMap(map); //营业额
        Integer validOrderCount = orderMapper.countByMap(map);//有效订单数
        Double orderCompletionRate = totalOrderNumber > 0 ? validOrderCount.doubleValue() / totalOrderNumber : 0; //订单完成率
        Double unitPrice = validOrderCount > 0 ? turnover / validOrderCount : 0; //平均客单价

        DecimalFormat df = new DecimalFormat("#.00");
        String formattedNumber = df.format(orderCompletionRate);
        String formattedNumber2 = df.format(unitPrice);
        //封装数据
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(Double.parseDouble(formattedNumber2))
                .orderCompletionRate(Double.parseDouble(formattedNumber))
                .validOrderCount(validOrderCount)
                .turnover(turnover == null ? 0 : turnover).build();
    }

    /**
     * 查询订单管理数据
     *
     * @return vo
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        Integer allOrders = orderMapper.countAll(); //全部订单数
        Integer cancelledOrders = orderMapper.countStatus(Orders.CANCELLED);//已取消数量
        Integer completedOrders = orderMapper.countStatus(Orders.COMPLETED);//已完成数量
        Integer deliveredOrders = orderMapper.countStatus(Orders.CONFIRMED);//待派送数量
        Integer waitingOrders = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);//待接单数量
        //封装数据
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders).build();
    }

    /**
     * 查询套餐总览
     *
     * @return VO
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE); //已停售套餐数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE); //已启售套餐数量
        //封装数据
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold).build();
    }

    /**
     * 查询菜品总览
     *
     * @return vo
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        return DishOverViewVO.builder()
                .discontinued(dishMapper.countByStatus(StatusConstant.DISABLE))
                .sold(dishMapper.countByStatus(StatusConstant.ENABLE)).build();
    }
}
